<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import http from '@/utils/http'

const visible = ref(false)
const sectionRef = ref(null)

const newsList = ref([])
const eventList = ref([])

function formatDate(str) {
  const d = new Date(str)
  return d.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'short',
    day: 'numeric'
  })
}

let observer

onMounted(async () => {
  // 动画触发
  observer = new IntersectionObserver(
    ([entry]) => {
      if (entry.isIntersecting) {
        visible.value = true
        observer.disconnect()
      }
    },
    { threshold: 0.3 }
  )
  if (sectionRef.value) observer.observe(sectionRef.value)

  // 获取新闻数据
  try {
    const res = await http.get('/news')
    const all = res.data
    console.log(all)


    newsList.value = all.filter(item => item.NewsType === '新闻资讯')
    eventList.value = all.filter(item => item.NewsType === '活动预告')
    // console.log(eventList.value)
  } catch (err) {
    console.error('获取新闻失败', err)
  }
})

onBeforeUnmount(() => {
  if (observer) observer.disconnect()
})
</script>


<template>
  <section ref="sectionRef" class="news-events" :class="{ visible }">
    <!-- 最新资讯 -->
     <div class="section-header">
  <h2 class="title">新闻资讯</h2>
  <router-link to="/news/all" class="more-btn">查看更多新闻</router-link>
</div>
<div class="card-grid">
  <div v-for="news in newsList.slice(0,2)" :key="news.NewsID" class="card">
    <img :src="news.ImageURL || '/mock-bg.jpg'" class="card-img" alt="新闻封面" />
    <div class="card-content">
      <h3 class="card-title">{{ news.Title }}</h3>
      <p class="card-summary">{{ news.Summary }}</p>
      <span class="card-date">{{ formatDate(news.ReleaseDate) }}</span>
    </div>
  </div>
</div>
<router-link to="/news/all" class="more-btn">查看更多新闻</router-link>

<!-- 活动预告 -->
 <div class="section-header">
  <h3 class="title">活动预告</h3>
  <router-link to="/events/all" class="more-btn">查看更多活动</router-link>
</div>
<div class="card-grid">
  <div v-for="event in eventList.slice(0,2)" :key="event.NewsID" class="card">
    <img :src="event.ImageURL || '/mock-bg.jpg'" class="card-img" alt="活动封面" />
    <div class="card-content">
      <h3 class="card-title">{{ event.Title }}</h3>
      <p class="card-summary">{{ event.Summary }}</p>
      <span class="card-date">活动时间：{{ formatDate(event.ReleaseDate) }}</span>
    </div>
  </div>
</div>
<router-link to="/events/all" class="more-btn">查看更多活动</router-link>

  </section>
</template>


<style scoped>
.section-header {
  position: relative;
  text-align: center; /* 标题居中 */
  margin-bottom: 24px;
}

.section-header .title {
  margin: 0;
  font-size: 28px;
  font-weight: 600;
  color: #2e1c0e;
  display: inline-block; /* 让标题宽度自适应 */
}

.section-header .more-btn {
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  font-size: 14px;
  padding: 8px 16px;
  color: #c2956d;
  border: 1px solid #c2956d;
  border-radius: 24px;
  background-color: transparent;
  text-decoration: none;
  transition: all 0.3s ease;
}

.section-header .more-btn:hover {
  background-color: #c2956d;
  color: white;
}

.title {
  text-align: center;
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 24px;
  color: #2e1c0e;
}
.news-events {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 24px;
  opacity: 0;
  transform: translateY(60px);
  transition: all 1s ease;
  width: 1000px;
}
.news-events.visible {
  opacity: 1;
  transform: translateY(0);
}

.block {
  margin-bottom: 80px;
}

.block-title {
  font-size: 28px;
  font-weight: 600;
  text-align: center;
  color: #2e1c0e;
  margin-bottom: 36px;
  position: relative;
}
.block-title::after {
  content: '';
  display: block;
  width: 64px;
  height: 3px;
  background: linear-gradient(to right, #c2956d, #e4c29a);
  border-radius: 4px;
  margin: 12px auto 0;
  opacity: 0.85;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 32px;
  margin-bottom: 50px;
}

.card {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.06);
  display: flex;
  flex-direction: column;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.card:hover {
  transform: translateY(-4px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.08);
}

.card-img {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.card-content {
  padding: 24px;
  text-align: center;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 12px;
}

.card-summary {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
  margin-bottom: 12px;
}

.card-date {
  font-size: 12px;
  color: #999;
}
</style>
